<template>
  <div class="checkout-address">
       <div class="text">
      <div v-if="!showAddress" class="none">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li><span>收<i/>货<i/>人：</span>{{showAddress.receiver}}</li>
        <li><span>联系方式：</span>{{showAddress.contact}}</li>
        <li><span>收货地址：</span>{{showAddress.fullLocation.replace(/ /g,'')+showAddress.address}}</li>
      </ul>
      <a  v-if="showAddress" href="javascript:;" @click="openAddressEdit(showAddress)">修改地址</a>
    </div>
    <div class="action">
      <XtxButton @click="openAddressList" class="btn">切换地址</XtxButton>
      <XtxButton @click="openAddressEdit()" class="btn">添加地址</XtxButton>
    </div>
    <AddressEdit @success="successHandler" ref="addressEdit"/>
     <!-- 切换收货地址 -->
  <XtxDialog title="切换收货地址" v-model="dialogVisible">
    <div :class="{scroll:list.length > 4}">
      <div
        class="text item"
        v-for="item in list"
        :key="item.id"
        :class="{ active: item.id === activeId  }"
        @click="changeAddress(item.id)"
      >
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
          <i @click="delAddressFn(item.id)" class="iconfont icon-close-new"></i>
        </ul>
      </div>
    </div>
      <template v-slot:footer>
        <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton @click="confirmAddress" type="primary">确认</XtxButton>
      </template>
  </XtxDialog>
  </div>
</template>
<script>
import Message from '@/components/library/Message'
import { delAddress } from '@/api/order'
import { computed, inject, ref, watch } from 'vue'
import AddressEdit from './address-edit.vue'
export default {
  components: { AddressEdit },
  name: 'CheckoutAddress',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    // 新的收货地址
    const newAddress = ref(null)
    // 显示到页面中的收货地址
    const showAddress = computed(() => {
      // 如果没有添加或修改新的收货地址
      if (!newAddress.value) {
        // 选择默认地址
        return props.list.find(item => item.isDefault === 0)
      } else {
        // 有新地址就显示新地址
        return newAddress.value
      }
    })
    // 对话框显示隐藏
    const dialogVisible = ref(false)
    // 添加收货地址组件
    const addressEdit = ref(null)
    const openAddressEdit = (showAddress) => {
      addressEdit.value.open()
      // console.log(addressEdit.value.formData)
      // console.log(showAddress.value)
      if (showAddress) {
        // 情况1：修改 - 填充表单
        // console.log('修改', address)
        for (const key in showAddress) {
          addressEdit.value.formData[key] = showAddress[key]
        }
      } else {
        // 情况2：添加 - 清空表单
        // console.log('添加', address)
        for (const key in addressEdit.value.formData) {
          addressEdit.value.formData[key] = ''
        }
        addressEdit.value.formData.isDefault = 0
        // console.log(123)
      }
    }
    const successHandler = (formData) => {
      // console.log(formData)
      if (formData) {
        // console.log(123)
        newAddress.value = { ...formData }
      }
    }
    // 切换地址列表的默认高亮
    const activeId = ref(null)
    // 点击切换高亮
    const changeAddress = (theId) => {
      // console.log(theId)
      activeId.value = theId
    }
    const data = inject('data')
    // 添加新地址后 切换地址中的默认高亮的更新
    watch(showAddress, (val) => {
      if (val && val.id) {
        activeId.value = val.id // 高亮
        data.addressId = val.id // 提交订单的id
      }
    }, { immediate: true })
    // 切换收货地址
    const confirmAddress = () => {
      newAddress.value = props.list.find(item => item.id === activeId.value)
      dialogVisible.value = false
    }

    const getAddressList = inject('getAddressList')
    const delAddressFn = async (id) => {
      // console.log(id)
      await delAddress(id)
      Message({ text: '删除收货地址成功', type: 'success' })
      const { userAddresses } = await getAddressList()
      // console.log(userAddresses)
      newAddress.value = userAddresses[0]
    }
    const openAddressList = () => {
      dialogVisible.value = true
      getAddressList()
    }
    return { showAddress, dialogVisible, addressEdit, openAddressEdit, successHandler, changeAddress, confirmAddress, activeId, delAddressFn, openAddressList }
  }
}
</script>
<style scoped lang="less">
.scroll{
      max-height: 500px;
      overflow:scroll;
      overflow-x: hidden;
}
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      position: relative;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
      i {
          position: absolute;
          top: 0;
          right: 0;
          opacity: 0;
          color: #666;
          transition: all .5s;
      }
      &:hover {
        i {
          opacity: 1;
          cursor: pointer;
        }
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
